package com.zhongjh.imageingstudy;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.zhongjh.imageingstudy.core.ImageMode;
import com.zhongjh.imageingstudy.view.GridRelativeLayout;
import com.zhongjh.imageingstudy.view.image.ActionImageView;

/**
 * 裁剪的Activity
 */
public class ClipActivity extends AppCompatActivity {

    ActionImageView actionImageView;
    Button btnCrop;
    Button btnSubmit;
    Button btnUndo;
    Button btnRedo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crop);
        allFindViewById();
        initImage();
        addAllListener();
    }

    private void allFindViewById() {
        actionImageView = findViewById(R.id.actionImageView);
        btnCrop = findViewById(R.id.btnCrop);
        btnSubmit = findViewById(R.id.btnSubmit);
        btnUndo = findViewById(R.id.btnUndo);
        btnRedo = findViewById(R.id.btnRedo);
    }

    private void addAllListener() {
        btnCrop.setOnClickListener(view -> clip());
        btnSubmit.setOnClickListener(v -> actionImageView.doClip());
        btnUndo.setOnClickListener(view -> actionImageView.doUndo());
        btnRedo.setOnClickListener(view -> clip());
    }

    private void initImage() {
        WindowManager wm = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
        Display display;
        int width, height;
        Point size = new Point();

        if (wm != null) {
            display = wm.getDefaultDisplay();
            display.getSize(size);
        }

        width = size.x;
        height = size.y;

        Glide.with(getApplication()).asBitmap().override(width, height).load(R.drawable.ic_lau2).into(new CustomTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                actionImageView.setImageBitmap(resource);
            }

            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {
            }
        });
    }

    private void clip() {
        Log.i("tag", "clip");
        preHide();
        actionImageView.setMode(ImageMode.CLIP);
//        cropImageView.setRatioCropRect(actionImageView.getCurrentRotateRectF(), -1);
//        cropImageView.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏相关控件
     */
    private void preHide() {

    }

}
